// pages/ReservationForm/ReservationForm.js
var request = require("../../utils/request.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userList: [],
    doctorList: [{
      id: 3,
      name: "专家团"
    }],
    isPicker: true,
    projectList: [],
    s: "2",
    name: "",
    focus: false,
    doctorName: "",
    doctorNames:'',
    user: "",
    project: "",
    multiIndex: [0, 0],
    multiArray: [],
    birthtime: "",
    times: "请先选择医生",
    mobile:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.options);
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    if(this.options.dateType=="doctor"){
      this.setData({
        name: options.name,
        times:options.day+" "+options.time,
        doctorNames:options.doctorName,
        isPicker:false
      })
      this.getDoctorTime({
        cid: options.cid
      });
    }else{
      this.setData({
        name: options.name
  
      })
    }
    this.setData({
      options:this.options
    })
    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},
  //下拉框选择事件
  change: function (e) {
    console.log(e);
    var m ,b;
    for(var i = 0;i<this.data.userList.length;i++){
      if(this.data.userList[i].id == e.detail){
        m = this.data.userList[i].mobile;
        b = this.data.userList[i].birth;
      }
    }
    this.setData({
      user: e.detail,
      mobile:m,
      birthtime:b
    })
  },
  change1: function (e) {
   // console.log(e)
   // console.log(this.data.projectList);
    this.setData({
      project: this.data.projectList[e.detail].name
    })


  },
  //预约医生选择 获取预约时间列表
  change2: function (e) {
   // console.log(e)
    this.setData({
      doctorName: e.detail,
      isPicker: false,
      times:"请选择"
    })
    this.getDoctorTime({
      cid: e.detail
    });
  },
  //预约获取基本信息
  ReservationForm: function (option) {
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointmentInfo', {
      cid: option.cid
    }, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        console.log(res)
        var p = [];
        for (var i = 0; i < res.projectList.length; i++) {
          var r = {
            "id": i,
            "name": res.projectList[i]
          }
          p.push(r)
        }
        for(var i = 0;i<res.doctorList.length;i++){
          if(res.doctorList[i].name == that.data.doctorNames){
            
            that.setData({
              doctorName:res.doctorList[i].id
            })
          }
        }
        that.setData({
          doctorList: res.doctorList,
          projectList: p,
          userList: res.userList,
        })
        console.log(that.data)
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //获取医生出诊时间
  getDoctorTime: function (option) {
    console.log(option);
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/clinicDoctorTime', {
      cdId: option.cid
    }, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        //console.log(res);
        var day = [];
        var hours = [];
        var temp = [];
        for (var i = 0; i < res.timeList.length; i++) {
          day.push(res.timeList[i].day);

        }
        for (var j = 0; j < res.timeList[0].hourList.length; j++) {
          hours.push(res.timeList[0].hourList[j].start + "-" + res.timeList[0].hourList[j].end);
        }
        that.setData({
          multiArray: [day, hours]
        })
        //console.log(that.data)
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },
  //选择时间段
  bindMultiPickerChange: function (e) {
    console.log(e);
    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    this.setData({
      times: year + " " + month,
      multiIndex: [0, 0]
    })
  },
  bindTimeChange: function (e) {
    //console.log(e);
    this.setData({
      birthtime: e.detail.value
    })
  },
  //获取焦点
  inputFocus: function () {
    this.setData({
      focus: true
    })
  },
  //失去焦点
  inputblur: function () {
    this.setData({
      focus: false
    })
  },
  formSubmit(e) {
    console.log(e.detail.value)
    // for(var i = 0;i<this.data.doctorList.length;i++){
    //   if(this.data.doctorList[i].name == e.detail.value.)
    // }
    var that = this
    if (e.detail.value.user == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请选择预约人',
        timeout: true
      });
      return false
    }
    if (e.detail.value.mobile == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请填写联系方式',
        timeout: true
      });
      return false
    } else {
      const regex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!regex.test(e.detail.value.mobile) || e.detail.value.mobile.length != 11) {
        this.toastDialog.showDialog({
          code: '1',
          message: "手机号码格式错误",
          timeout: true
        });
        return false
      }
    }
    if (e.detail.value.project == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请填写预约项目',
        timeout: true
      });
      return false
    }
    if (e.detail.value.doctorId == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请填写预约医生',
        timeout: true
      });
      return false
    }
    if (e.detail.value.desc == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请填写症状描述',
        timeout: true
      });
      return false
    }
    if (e.detail.value.desc == '') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请填写症状描述',
        timeout: true
      });
      return false
    }
    if (e.detail.value.date == '请选择') {
      that.toastDialog.showDialog({
        code: 1,
        message: '请选择预约时间',
        timeout: true
      });
      return false
    }
    var that = this;
    that.toastDialog.showDialog({
      code: 'loading',
      message: '加载中',
      timeout: false
    }); //loading 加载框
    request.postData('xcx/appointment', e.detail.value, function (res) {
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == 0) {
        console.log(res)
        that.toastDialog.showDialog({
          code: res.code,
          message: '提交成功',
          tips: '请在预约记录中确认',
          timeout: true
        });
        wx.redirectTo({
          url: '../AppointmentRecord/AppointmentRecord'
        })
      } else {
        that.toastDialog.showDialog({
          code: res.code,
          message: res.message,
          timeout: true
        });
      }
    }, function (res) {
      that.toastDialog.showDialog({
        code: res.code,
        message: res.message,
        timeout: true
      });
    });
  },

  formReset(e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    // this.setData({
    //   chosen: ''
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.ReservationForm(this.data.options);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})